package cn.edu.nchu.fragmentdemo.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import cn.edu.nchu.fragmentdemo.R;
import cn.edu.nchu.fragmentdemo.MainActivity;

import java.io.IOException;
import java.lang.reflect.Field;


public class CommenUtils {
    Context context = null;
    /*private final int mapwidth = MyConfig.getInstance().getScreenWidth();
    private final int hight = MyConfig.getInstance().getScreenHight();*/
    RelativeLayout mapLayout;
    LinearLayout operationLayout_Right;
    LinearLayout operationLayout_Left;
    MainActivity mainActivity;
    public static volatile CommenUtils commenUtils = null;


    /**
     * @param context
     * @return
     */
    public static CommenUtils getInstance(Context context) {
        if (commenUtils == null) {
            return new CommenUtils(context);

        } else {
            return commenUtils;
        }

    }

    public CommenUtils() {

    }

    //充满屏幕
    /*public  void setMapwidth(){
        LinearLayout linearLayout = (LinearLayout) MainActivity.getActivity().findViewById(R.id.activity_main_option_right) ;
        LinearLayout.LayoutParams rightLayoutParams =
                new LinearLayout.LayoutParams(0,hight,0);
        linearLayout.setLayoutParams(rightLayoutParams);
    }*/
    //sing a song - 是想你的声音啊 - 你快听 滴答滴 滴答滴 滴答滴 是雨滴的声音 你快听 是我在 是我在 是我在哼你最爱听的旋律
    public CommenUtils(Context context) {
        MainActivity mainActivity = (MainActivity) MainActivity.getActivity();
//        mapLayout = mainActivity.findViewById(R.id.activity_map_layout);
//        //myFragmentManager = new MyFragmentManager();
//        operationLayout_Left = mainActivity.findViewById(R.id.activity_main_option_left);
//        operationLayout_Right = mainActivity.findViewById(R.id.activity_main_option_right);
        mainActivity = (MainActivity) MainActivity.getActivity();
        this.context = context;
    }

    /**
     * 通过Bitmapdrawable获取Bitmap
     *
     * @param context
     * @param fileName
     * @return
     */
    public Bitmap getBitmapFromDrawable(Context context, String fileName) {
        BitmapDrawable bitmapDrawable = null;
        try {
            bitmapDrawable = new BitmapDrawable(context.getAssets().open(fileName));
            Bitmap mBitmap = bitmapDrawable.getBitmap();
            return mBitmap;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过资源ID获取Bitmap
     *
     * @param resources
     * @param resId
     * @return
     */
    public Bitmap getBitmapFromResource(Resources resources, int resId) {
        return BitmapFactory.decodeResource(resources, resId);
    }


    /**
     * @param pathName
     * @return
     */
    public Bitmap getBitmapFromFile(String pathName) {
        return BitmapFactory.decodeFile(pathName);
    }

    /**
     * 获取resourse文件
     */

    public int getResource(String iconName) {
        try {
            //    int i = context.getResources().getIdentifier(iconName, "drawable",mainActivity.getPackageName());
            Field field = R.drawable.class.getField(iconName);

            int i = field.getInt(R.drawable.class);
            return i;
        } catch (Exception e) {
            Log.e("icon", e.toString());
        }
        return -1;
    }

    /**
     * 获取view的id
     */

    public int getViewID(String name) {
        try {
            int i = context.getResources().getIdentifier("name", "id", mainActivity.getPackageName());
            return i;
        } catch (Exception e) {
            Log.e("icon", e.toString());
        }
        return -1;
    }

    /**
     * 返回 指定的 String 是否是 有效的 IP 地址.
     */
    public static boolean isValidIpAddress(String value) {
        int start = 0;
        int end = value.indexOf('.');
        int num = 0;
        while (start < value.length()) {
            if (-1 == end) {
                end = value.length();
            }
            try {
                int block = Integer.parseInt(value.substring(start, end));
                if ((block > 255) || (block < 0)) {
                    return false;
                }
            } catch (NumberFormatException e) {
                return false;
            }
            num++;
            start = end + 1;
            end = value.indexOf('.', start);
        }
        return num == 4;
    }
}
